package cn.swh.guguanjia.mapper;

import cn.swh.guguanjia.entity.SysArea;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/10/30 13:12
 * @Version: 1.0
 */
public class SysAreaProvider {
    public String selectPage(Map<String, Object> map) {
        return new SQL() {{
            SELECT("sub.*,parent.NAME parent_name");
            FROM("sys_area sub");
            LEFT_OUTER_JOIN("sys_area parent ON sub.parent_id = parent.id");
            WHERE("sub.del_flag=0");
            if (map.containsKey("sid") && !StringUtils.isEmpty(map.get("sid"))) {
                WHERE("sub.parent_ids like CONCAT('%,',#{sid},',%') or sub.id=#{sid}");
            } else if (map.containsKey("name") && !StringUtils.isEmpty(map.get("name"))) {
                WHERE("sub.name like CONCAT('%',#{name},'%')");
            }
        }}.toString();
    }


    public String insetBatch(@Param("areas") List<SysArea> areas){
        return new SQL(){{
            INSERT_INTO("`sys_area`");
            INTO_COLUMNS("`parent_id`, `parent_ids`, `code`, `name`, `type`, `create_by`, `create_date`, " +
                    "`update_by`, `update_date`, `remarks`, `del_flag`, `icon`");
            for (int i = 0; i <areas.size() ; i++) {
                StringBuffer sb = new StringBuffer();
                sb.append("#{areas[").append(i).append("].parentId},#{areas[").append(i)
                        .append("].parentIds},#{areas[").append(i).append("].code},#{areas[").append(i)
                        .append("].name},#{areas[").append(i).append("].type},#{areas[").append(i)
                        .append("].createBy},#{areas[").append(i).append("].createDate},#{areas[").append(i)
                        .append("].updateBy},#{areas[").append(i).append("].updateDate},#{areas[").append(i)
                        .append("].remarks},#{areas[").append(i).append("].delFlag},#{areas[").append(i).append("].icon}");

                INTO_VALUES(sb.toString());
                //动态添加values括号
                ADD_ROW();
            }
        }}.toString();
    }


}
